#!/bin/bash
set -ue

sleep 10
PORT=51002
#IP=$1
#SIZE=$2
#PASSWORD=$3
slot_num=$((16384 / ${SIZE}))
for (( i = 0; i < ${SIZE}; i++ )); do
        ./redis-cli -h ${IP}.10 -p ${PORT} -a ${PASSWORD} cluster meet ${IP}.$((10 + $i)) ${PORT}
        ./redis-cli -h ${IP}.10 -p ${PORT} -a ${PASSWORD} cluster meet ${IP}.$((20 + $i)) ${PORT}
done

echo "cluster meet done......"
sleep 10

./redis-cli -h ${IP}.10 -p ${PORT} -a ${PASSWORD}  cluster nodes
for (( i = 0; i < ${SIZE}; i++ )); do
        uuid=$(./redis-cli -h ${IP}.10 -p ${PORT} -a ${PASSWORD}  cluster nodes | grep ${IP}.$((10 + $i)) | awk '{print $1}')
        ./redis-cli -h ${IP}.$((20 + $i)) -p ${PORT}  -a ${PASSWORD} cluster replicate ${uuid}
done
for (( i = 0; i < $((${SIZE} - 1 )); i++ )); do
        ./redis-cli -h ${IP}.$((10 + $i)) -p ${PORT} -a ${PASSWORD} cluster addslots {$((${i}*${slot_num}))..$((${slot_num}*(${i}+1)-1))}
done
./redis-cli -h ${IP}.$((10 + ${SIZE} - 1)) -p ${PORT} -a ${PASSWORD} cluster addslots {$(((${SIZE}-1)*${slot_num}))..16383}


echo "cluster build done.."
sleep 5
./redis-cli -h ${IP}.10 -p ${PORT}  -a ${PASSWORD} cluster nodes